const {
  TaskProxy,
  PushProxy,
  HistoryProxy
} = require('../proxys')

module.exports = class PushController {
  static async push (ctx) {
    const { id } = ctx.params
    const task = await TaskProxy.findOne({ _id: id })
    if (task) {
      const result = await PushProxy.push(task)
      const { status, data } = result
      if (status === 200) {
        const result = data.sendno === '0' ? 0 : -1
        const history = {
          time: Date.now(),
          name: task.name,
          songs_id: task.id,
          msg_id: data.msg_id,
          status: result
        }
        await HistoryProxy.newAndSave(history)
        ctx.body = {
          code: 0,
          msg: '推送成功'
        }
      }
    }
  }
}
